const setting = require("../dao/settingDao");
const validate = require("validate.js");
const { ValidationError } = require("../utils/errors");
//修改全局配置
const setSetting = async (info) => {
  const rule = {
    avatar: {
      type: "string",
    },
    siteTitle: {
      type: "string",
    },
    github: {
      type: "string",
    },
    qq: {
      type: "string",
    },
    qqQrCode: {
      type: "string",
    },
    weixin: {
      type: "string",
    },
    weixinQrCode: {
      type: "string",
    },
    mail: {
      type: "string",
    },
    icp: {
      type: "string",
    },
    githubName: {
      type: "string",
    },
    favicon: {
      type: "string",
    },
  };
  const result = validate.validate(info, rule);

  if (!result) {
    const data = await setting.setSetting(info);
    return data;
  } else {
    console.log(result);
    throw new ValidationError("数据验证失败");
  }
};

//获取全局配置
const getSetting = async () => {
  const data = await setting.getSetting();
  return data;
};

module.exports = {
  setSetting,
  getSetting,
};
